﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class FormFinder : Form
    {
        public FormFinder()
        {
            InitializeComponent();
        }

        public List<TableWithKey> AllTables { get; internal set; }

        private void button1_Click(object sender, EventArgs e)
        {
            var allTables = AllTables;
            var strs = this.textBox1.Text.Split(',');

            this.listView1.Items.Clear();
            for (var j = 0; j < strs.Length; j++)
            {
                for (var i = 0; i < allTables.Count; i++)
                {
                    var res=allTables[i].FindColumnByData(strs[j]);
                    if (res.Count == 0) continue;

                    for(var k=0;k<res.Count; k++)
                    {
                        var rowIndex = res[k][0];
                        var clmIndex = res[k][1];
                        var lvi = new ListViewItem(allTables[i].TableData.TableName);
                        lvi.SubItems.Add(allTables[i].TableData.RefDataTable.Columns[clmIndex].ColumnName);
                        lvi.SubItems.Add(rowIndex.ToString());
                        lvi.SubItems.Add(allTables[i].TableData.RefDataTable.Rows[rowIndex][clmIndex].ToString());

                        this.listView1.Items.Add(lvi);
                    }
                   
                }
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var tableName = this.listView1.SelectedItems[0].Text;
            for (var i = 0; i < AllTables.Count; i++)
            {
                if (tableName == AllTables[i].TableData.TableName)
                {
                    ((AllTables[i].Parent as TabPage).Parent as TabControl).SelectedTab = AllTables[i].Parent as TabPage;
                }
            }
        }
    }
}
